package nettyTest.netty3Test.codecTest1.codeTest1;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.concurrent.TimeUnit;

public class CodeClient {

    private static final Logger logger = LogManager.getLogger(CodeClient.class);

    public static void main(String[] args) {
        EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast(new MyMessageToMessageCodec2());
                p.addLast(new MyMessageToMessageCodec());
                p.addLast(new CodeClientHandler());
            }

        });
        ChannelFuture sync = null;
        try {
            sync = bootstrap.connect("127.0.0.1", 8023).sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

//        ChannelFuture finalSync = sync;
//        sync.channel().eventLoop().schedule(()->{
//            finalSync.channel().close();
//        }, 10, TimeUnit.SECONDS);
//
//        try {
//            sync.channel().closeFuture().addListener(future -> {
//                logger.info("client close fff");
//            }).sync();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }
}
